<?php
namespace Macaca\Controller\Admin;

use Macaca;

class ArticleCategory extends Base {

    public function moduleAC() {
        return new Macaca\Module\Admin\ArticleCategory($this->app);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new Macaca\View\Admin\ArticleCategory($app);
        $module = $this->moduleAC();
        $linker = $this->linker;

        $data['title'] = 'Macaca 栏目管理';
        $data['url_add'] = $linker->toFix('/admin/article/category/add');
        $data['url_edit'] = $linker->to('/admin/article/category/edit');
        $data['url_delete'] = $linker->to('/admin/article/category/delete');
        $data['url_browse'] = $linker->to('/admin/article/category/browse');

        $data['ac'] = $module->getSubCategory(0);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\ArticleCategoryEditor($app);
        $module = $this->moduleAC();

        $data['title'] = '添加栏目';
        $data['url_save'] = $this->linker->toFix('/admin/article/category/save');

        $data['ac'] = $n = $module->getEmptyItem();
        $data['ac_parent_list'] = $module->getParentList($n['ac_id'], $n['parent_id']);

        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\ArticleCategoryEditor($app);
        $module = $this->moduleAC();

        $data['title'] = '编辑栏目';
        $data['url_save'] = $this->linker->toFix('/admin/article/category/save');

        $acId = $app->router()->getParameter('ac_id');
        $data['ac'] = $n = $module->getItemById($acId);
        $data['ac_parent_list'] = $module->getParentList($n['ac_id'], $n['parent_id']);

        if (empty($data['ac'])) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的栏目不存在！';
        }
        else {
            $this->response->setBody($view->render($data));
        }
    }

    public function actionDeleteItem() {
        $module = $this->moduleAC();
        $nid = $this->app->router()->getParameter('ac_id');

        if ($module->deleteItemById($nid)) {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选栏目。';
        }
        else {
            echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;栏目删除失败!';
        }
    }

    public function actionBrowseItem() {
        echo '<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;该功能未实现!';

    }

    public function actionSaveItem() {
        $module = $this->moduleAC();
        if ($module->saveItem($_POST)) {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;栏目保存成功!');
        }
        else {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;栏目保存失败!');
        }

    }
}